﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CCWin;
using System.Text.RegularExpressions;

namespace SCCM
{
    public partial class AlertLimitForm : Skin_Mac
    {
        public AlertLimitForm()
        {
            InitializeComponent();
        }
        private int id = -1;
        private void AlertLimitForm_Load(object sender, EventArgs e)
        {
            initUI();
        }
        private void initUI(){
            DataBase db = new DataBase();
            DataTable dt = db.SelectLimits();
            if (dt.Rows.Count == 0) return;

            id = (int)dt.Rows[0]["id"];
            NO2.Text = (double)dt.Rows[0]["NO2"] + "";
            CO.Text = (double)dt.Rows[0]["CO"] + "";
            NO.Text = (double)dt.Rows[0]["NO"] + "";
            O3.Text = (double)dt.Rows[0]["O3"] + ""; 
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (CheckTxt(NO2.Text) && CheckTxt(CO.Text) && CheckTxt(NO.Text) && CheckTxt(O3.Text))
            {

                string no2 = NO2.Text;
                string co = CO.Text;
                string no = NO.Text;
                string o3 = O3.Text;
                DataBase db = new DataBase();
                db.UpdateLimites(id, Convert.ToDouble(no2), Convert.ToDouble(co), Convert.ToDouble(no), Convert.ToDouble(o3));

                initUI();
                MessageBox.Show("修改成功");
            }
            else {
                MessageBox.Show("输入格式不正确，请输入数字格式");
            }
        }

        private bool CheckTxt(string i)
        {
            Regex regAll = new Regex("^-?\\d+(\\.\\d+)*$");
            if (regAll.IsMatch(i))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
